package com.fingard.dsp.bank.directbank.jstzrcb01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * @author dicke
 * @date 2018/9/16 11:01
 */
public class JSTZRCB01Balance extends JSTZRCB01Base implements IBalance {


    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        RetAtsBalDetail detail = new RetAtsBalDetail();
        retAts.writeStart();

        XmlTextWriter xmlWriter = new XmlTextWriter();
        JSTZRCB01Header header = new JSTZRCB01Header();
        String OpName = "B2EActBalQry";
        header.transCode = OpName;

        xmlWriter.autoFormat = false;

        while (p_req7506.readRecord()) {
            header.reqSeqNo = p_req7506.reqHeader.transSeqID;
            writeCommonHead(xmlWriter,header);
            xmlWriter.writeStartElement("Body");
            xmlWriter.writeStartElement("List");
            XElement tmpXElement = new XElement("Map", "");
            tmpXElement.addChild(new XElement("AcNo", p_req7506.readedDetail.actNum));
            xmlWriter.writeXElement(tmpXElement);
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();

            String[] retStrArr = sendToBank(xmlWriter.toXmlString(), OpName);
            if (retStrArr[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(retStrArr[1], tmpSbMsg);

                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getSimpleText("IBSReturnCode");
                    String tmpRetInfo = tmpBkReader.getSimpleText("IBSReturnMsg");
                    if (BANK_RET_RIGHT_CODE.equals(tmpRetCode)) {
                        detail = new RetAtsBalDetail();
                        detail.respCode = "0";
                        detail.respInfo = tmpRetInfo;
                        //需要转换
                        detail.actCur  = tmpBkReader.getSimpleText("Currency");
                        detail.availBal = tmpBkReader.getSimpleText("AvailBal");
                        detail.currBal = tmpBkReader.getSimpleText("Balance");
                        retAts.writeRetDetail(detail, p_req7506.isBatch);
                    } else {
                        detail.respCode = tmpRetCode;
                        detail.respInfo = tmpRetInfo;
                        retAts.writeRetDetail(detail, p_req7506.isBatch);
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpSbMsg.toString();
                    retAts.writeRetDetail(detail, p_req7506.isBatch);
                }
            } else {
                detail.respCode = "-1";
                detail.respInfo = retStrArr[1].toString();
                retAts.writeRetDetail(detail, p_req7506.isBatch);
            }
        }

        retAts.writeEnd();
        return retAts;
    }

        @Override
        public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
            return null;
        }
    }
